(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;
	var currentPage = 1;
	var pageSize = 10;
	var totalSize;
	var now = 0;
	var dataCache = [];
	var classCaches = null;
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
		keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			pagebtns();
			initGradeClass();
		});
	});

	function initGradeClass(){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var userRoles = $("#user-name",parent.parent.document).attr("rolecodes");
		var data = {
				cmd:"getDataRole",
				model:"license",
				userKey:userKey,
				data:{
					rolecode:userRoles
				},
				remark:"",
		};
		AjaxCrud.get2(serverUrl.ServerLoginUrl,data,function(res){
			classCaches = res;
			var typeOpt = "<option value='' >学段</option>";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var data = res.data;
				for(var i = 0; i < data.length; i++){
					var name = "";
					if(data[i].type == "0"){
						name = "幼儿园";
					}else if(data[i].type == "1"){
						name = "小学";
					}else if(data[i].type == "2"){
						name = "中学";
					}else if(data[i].type == "3"){
						name = "高中";
					}
					typeOpt += "<option value='"+data[i].type+"'>"+name+"</option>";
				}
				$("#student-type").html(typeOpt);
				
				$("#student-type").unbind("change");
				$("#student-type").change(function(){
					var gradeData = null;
					var type = $("#student-type option:selected").val();
					var gradeOpt = "<option value='' classcodes='' classes=''>年级</option>";
					if(type != ""){
						for(var i = 0; i < data.length; i++){
							if(data[i].type == type){
								gradeData = data[i].data;
							}
						}
						for(var i = 0; i < gradeData.length; i++){
							gradeOpt += "<option value='"+gradeData[i].grade+"' classcodes='"+gradeData[i].classcode+"' classes='"+gradeData[i].classes+"'>"+gradeData[i].grade+"年级</option>";
						}
					}
					$("#student-grade").html(gradeOpt);
					
					var classOpt = "<option value='' classes=''>班级</option>";
					$("#student-classcode").html(classOpt);
					initStudent(1,pageSize);
				});
				
				$("#student-grade").unbind("change");
				$("#student-grade").change(function(){
					var classes = $("#student-grade option:selected").attr("classes").split(",");
					var classcodes = $("#student-grade option:selected").attr("classcodes").split(",");
					var classOpt = "";
					for(var j = 0; j < classes.length; j++){
						if(classcodes[j] == "" && classes[j] == ""){
							classOpt += "<option value='' classes=''>班级</option>";
						}else{
							classOpt += "<option value='"+classcodes[j]+"' classes='"+classes[j]+"'>"+classes[j]+"班</option>";
						}
					}
					$("#student-classcode").html(classOpt);
					initStudent(1,pageSize);
				});
				$("#student-classcode").unbind("change");
				$("#student-classcode").change(function(){
					initStudent(1,pageSize);
				});
			}else{
			
			}
			initStudent(1,pageSize);
		},null,null);
	}

	function initStudent(currentpage,pagesize){
		currentPage = currentpage;
		var userKey = $("#user-name",parent.parent.document).attr("key");		
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var rolecode = $("#user-name",parent.parent.document).attr("rolecodes");
		var name = $("#student-name").val();
		var classcode = $("#student-classcode option:selected").val();
		var islock = $("#student-lock option:selected").val();
		var type = $("#student-type option:selected").val();
		var data = {
				cmd:"getAllStudentsByPage",
				model:"student",
				userKey:userKey,
				data:{
					currentpage:currentpage,
					pagesize:pagesize,
					name:encodeURI(name),
					classcode:classcode,
					del:islock,
					schoolcode:schoolcode,
					rolecode:rolecode,
					type:type
				},
				remark:"",
				
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var content = "";
			var pagecontent = "<div class=\"pages-btn-next\">&gt;&gt;</div>";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var data = res.data.data;
				var total = res.data.total;
				totalSize = Math.ceil(total/pagesize);
				if(totalSize < 5 && totalSize >0){
					for(var i = totalSize-1 ; i >= 0; i--){
						if(i == currentpage-1){
							pagecontent += "<div class=\"pages-btn page-btn-select\">"+(i + 1)+"</div>"; 
						}else{
							pagecontent += "<div class=\"pages-btn\">"+(i + 1)+"</div>"; 
						}
					}
				}else if(totalSize == 0){
					pagecontent += "<div class=\"pages-btn\">1</div>";
				}else if(totalSize > 5){
					var lastp = (now*5)+5;
					if(lastp > totalSize){
						lastp = totalSize;
					}
					for(var i = lastp-1 ; i >= (now*5); i--){
						if(i == currentpage-1){
							pagecontent += "<div class=\"pages-btn page-btn-select\">"+(i + 1)+"</div>"; 
						}else{
							pagecontent += "<div class=\"pages-btn\">"+(i + 1)+"</div>"; 
						}
					}
				}
				pagecontent += "<div class=\"pages-btn-last\">&lt;&lt;</div>";
				$("#student-page").empty();
				$("#student-page").append(pagecontent);
				
				$(".pages-btn").unbind("click");
				$(".pages-btn").click(function(){
					$(".pages-btn").removeClass("page-btn-select");
					$(this).addClass("page-btn-select");
					currentPage = parseInt($(this).text());
					initStudent(currentPage,pageSize);
				});
				
				$(".pages-btn-last").unbind("click");
				$(".pages-btn-last").click(function(){
					if(totalSize < 5 && totalSize >0){
						return false;
					}else if(totalSize == 0){
						return false;
					}else if(totalSize > 5){
						if(now > 0){
							now --;
							initStudent((now*5+1),pageSize);
						}else{
							return false;
						}
					}
				});
				
				$(".pages-btn-next").unbind("click");
				$(".pages-btn-next").click(function(){
					if(totalSize < 5 && totalSize >0){
						return false;
					}else if(totalSize == 0){
						return false;
					}else if(totalSize > 5){
						if(totalSize > (now+1)*5){
							now ++;
							initStudent((now*5+1),pageSize);
						}else{
							return false;
						}
					}
				});
				
				if(data.length > 0){
					dataCache = [];
					for(var i = 0; i < data.length; i++){
						for(var key in data[i]){
							if(data[i][key] == null || data[i][key] == "null" || data[i][key] == undefined){
								data[i][key] = "";
							}
						}
						var lockBtns = "";
						if(islock == 1){
							lockBtns = "<div class=\"table-edit\" dataindex = \""+i+"\" studentcode=\""+data[i].studentcode+"\" title=\"编辑\"></div>"+
							"<div class=\"table-locked\" studentcode=\""+data[i].studentcode+"\" title=\"锁定\"></div>";
						}else if(islock == 0){
							lockBtns = "<div class=\"table-unlocked\" studentcode=\""+data[i].studentcode+"\" title=\"解锁\"></div>"+
							"<div class=\"table-delete\" dataindex = \""+i+"\" imei=\""+data[i].imei+"\" studentcode=\""+data[i].studentcode+"\" title=\"删除\"></div>";
						}
						content += "<div class=\"table-trs\">" +
							"<div class=\"tables-headimg\">" +
								"<img src=\"../../../../../../"+ServiceName+data[i].headimg+"\" width=\"100%\" height=\"100%\"/>" +
							"</div>" +
							"<div class=\"tables-left\">" +
								"<div class=\"table-name\" title=\""+data[i].name+"\"\ style=\"width:150px;\">"+substr(data[i].name,8)+"</div>" +
								"<div class=\"table-code\" title=\""+data[i].studentcode+"\" style=\"width:150px;\">学号： "+substr(data[i].studentcode,10)+"</div>" +
							"</div>" +
							"<div class=\"tables-middle\">" +
								"<div class=\"table-class\" >"+data[i].grade+"年级"+data[i].clazz+"班</div>" +
								"<div class=\"table-code\">"+data[i].waytype+"</div>" +
							"</div>" +
							"<div class=\"tables-right\">" +
								"<div class=\"table-address\" title=\""+data[i].adress+"\">"+substr(data[i].adress,12)+"</div>" +
								"<div class=\"table-oprate\">" +
									"<div class=\"table-detail\" dataindex = \""+i+"\" studentcode=\""+data[i].studentcode+"\" title=\"详情\"></div>" +
									lockBtns +
								"</div>" +
							"</div>" +
						"</div>";
						dataCache.push(data[i]);
					}
					$("#student-data-show").empty();
					$("#student-data-show").append(content);
					
					$(".table-trs").unbind("hover");
					$(".table-trs").hover(function(){
						$(this).find(".table-oprate").show();
					},function(){
						$(".table-oprate").hide();
					});
					
					$(".table-detail").unbind("click");
					$(".table-detail").click(function(){
						var index = parseInt($(this).attr("dataindex"));
						$("#student-form-show").show();
						$("#right-tables").hide();
						$("#student-form-show").contents().find("input").attr("disabled","disabled");
						$("#student-form-show").contents().find("input").css("background-color","#E5E5E5");
						$("#student-form-show").contents().find("select").attr("disabled","disabled");
						$("#student-form-show").contents().find("select").css("background-color","#E5E5E5");
						$("#student-form-show").contents().find("#back-to-table").unbind("click");
						$("#student-form-show").contents().find("#back-to-table").click(function(){
							$("#student-form-show").hide();
							$("#right-tables").show();
						});
						showData(dataCache[index],"detail");
					});
					
					$(".table-edit").unbind("click");
					$(".table-edit").click(function(){
						var index = parseInt($(this).attr("dataindex"));
						$("#student-form-show").show();
						$("#right-tables").hide();
						$("#student-form-show").contents().find("input").removeAttr("disabled");
						$("#student-form-show").contents().find("input").css("background-color","#fff");
						$("#student-form-show").contents().find("select").removeAttr("disabled");
						$("#student-form-show").contents().find("select").css("background-color","#fff");
						$("#student-form-show").contents().find("#back-to-table").unbind("click");
						$("#student-form-show").contents().find("#back-to-table").click(function(){
							$("#student-form-show").hide();
							document.getElementById('student-form-show').contentWindow.location.reload(true);
							$("#right-tables").show();
						});
						showData(dataCache[index],"edit");
						
						check1("student-name",isNull,"null","student-form-show");
						check1("student-fname1",isNull,"null","student-form-show");
						check1("student-imei",isNull,"null","student-form-show");
						check1("student-cardcode",isNull,"null","student-form-show");
						check1("student-cellphone",checkMobilePhone,"checkAddNull","student-form-show");
						check1("student-fphone1",checkMobilePhone,"checkAddNull","student-form-show");
						check1("student-fcardid1",checkCard,"checkAddNull","student-form-show");
						check1("student-studentcode",checkNum,"checkAddNull","student-form-show");
						check1("student-inschooldate",isNull,"null","student-form-show");
						check1("student-birdate",isNull,"null","student-form-show");
						
						$("#student-form-show").contents().find("#save-btns").unbind("click");
						$("#student-form-show").contents().find("#save-btns").click(function(){
							if(checkSave()){
								if(check2()){
									updateStudent();
								}
							}
						});
						
					});
					
					$(".table-locked").unbind("click");
					$(".table-locked").click(function(){
						var studentcode = $(this).attr("studentcode");
						deleteBox("确定要锁定吗？",function(){
							lockStudent(studentcode);
						});
						
					});
					
					$(".table-delete").unbind("click");
					$(".table-delete").click(function(){
						var studentcode = $(this).attr("studentcode");
						var imei = $(this).attr("imei");
						deleteBox("确定要删除吗？",function(){
							deleteStudent(studentcode,imei);
						});
						
					});
					
					$(".table-unlocked").unbind("click");
					$(".table-unlocked").click(function(){
						var studentcode = $(this).attr("studentcode");
						var imei = $(this).attr("imei");
						var fphone1 = $(this).attr("fphone1");
						deleteBox("确定要解锁吗？",function(){
							unlockStudent(studentcode,imei,fphone1);
						});
					});
				}else{
					content += "<div class=\"table-trs\">" +
									"<div class=\"no-data-show\">没有查到相关结果！</div>" +
								"</div>";
					$("#student-data-show").empty();
					$("#student-data-show").append(content);
				}
			}else{
				content += "<div class=\"table-trs\">" +
								"<div class=\"no-data-show\">没有查到相关结果！</div>" +
							"</div>";
				$("#student-data-show").empty();
				$("#student-data-show").append(content);
			}
			initClassEdit();
		},null,null);	
	}
	function initClassEdit(){
		var typeOpt = "<option value='' >学段</option>";
		var gradeOpt = "<option value='' classcodes='' classes=''>年级</option>";
		var classOpt = "<option value='' classes=''>班级</option>";
		var res = classCaches;
		if(res.result == 1 && res.data != undefined && res.data != ""){
			var data = res.data;
			for(var i = 0; i < data.length; i++){
				var name = "";
				if(data[i].type == "0"){
					name = "幼儿园";
				}else if(data[i].type == "1"){
					name = "小学";
				}else if(data[i].type == "2"){
					name = "中学";
				}else if(data[i].type == "3"){
					name = "高中";
				}
				typeOpt += "<option value='"+data[i].type+"'>"+name+"</option>";
			}
			$("#student-form-show").contents().find("#student-periodtype").html(typeOpt);
			
			$("#student-form-show").contents().find("#student-periodtype").unbind("change");
			$("#student-form-show").contents().find("#student-periodtype").change(function(){
				var gradeData = null;
				var type = $("#student-form-show").contents().find("#student-periodtype option:selected").val();
				var gradeOpt = "<option value='' classcodes='' classes=''>年级</option>";
				if(type != ""){
					for(var i = 0; i < data.length; i++){
						if(data[i].type == type){
							gradeData = data[i].data;
						}
					}
					for(var i = 0; i < gradeData.length; i++){
						gradeOpt += "<option value='"+gradeData[i].grade+"' classcodes='"+gradeData[i].classcode+"' classes='"+gradeData[i].classes+"'>"+gradeData[i].grade+"年级</option>";
					}
				}
				$("#student-form-show").contents().find("#student-grade").html(gradeOpt);
				
				var classOpt = "<option value='' classes=''>班级</option>";
				$("#student-form-show").contents().find("#student-classcode").html(classOpt);
			});
			
			$("#student-form-show").contents().find("#student-grade").unbind("change");
			$("#student-form-show").contents().find("#student-grade").change(function(){
				var classes = $("#student-form-show").contents().find("#student-grade option:selected").attr("classes").split(",");
				var classcodes = $("#student-form-show").contents().find("#student-grade option:selected").attr("classcodes").split(",");
				var classOpt = "";
				for(var j = 0; j < classes.length; j++){
					if(classcodes[j] == "" && classes[j] == ""){
						classOpt += "<option value='' classes=''>班级</option>";
					}else{
						classOpt += "<option value='"+classcodes[j]+"' classes='"+classes[j]+"'>"+classes[j]+"班</option>";
					}
				}
				$("#student-form-show").contents().find("#student-classcode").html(classOpt);
			});		
		}else{
			$("#student-form-show").contents().find("#student-periodtype").html(typeOpt);
			$("#student-form-show").contents().find("#student-grade").html(gradeOpt);
			$("#student-form-show").contents().find("#student-classcode").html(classOpt);
		}
	}

	function lockStudent(studentcode){
		var userKey = $("#user-name",parent.parent.document).attr("key");	
		var data = {
				cmd:"bachDeleteStudents",
				model:"student",
				userKey:userKey,
				data:{
					studentcodes:[studentcode],
				},
				remark:""					
		};
		processBegin("正在锁定中···");
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("锁定成功");
				initStudent(currentPage,pageSize);
			}else{
				processError("锁定失败");
			}
		},null,null);
	}
	function deleteStudent(studentcode,imei){
		var userKey = $("#user-name",parent.parent.document).attr("key");	
		var param = {};
		param["studentcode"] = studentcode;
		param["imei"] = imei;
		var data = {
				cmd:"delete",
				model:"student",
				userKey:userKey,
				data:param,
				remark:""					
		};
		processBegin("正在删除中···");
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("删除成功");
				initStudent(currentPage,pageSize);
			}else{
				processError("删除失败");
			}
		},null,null);
	}
	function unlockStudent(studentcode,imei,fphone1){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var param = {};
		param["del"] = "1";
		param["studentcode"] = studentcode;
		param["imei"] = imei;
		param["fphone1"] = fphone1;
		
		var data = {
				cmd:"unLockStudent",
				model:"student",
				userKey:userKey,
				data:param,
				remark:"",						
		};
		processBegin("正在解锁中···");
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("解锁成功");
				initStudent(currentPage,pageSize);					
			}else{
				processError("解锁失败");
			}
		},null,null);
	}
	function substr(str,len){
		if(str != undefined){
			if(str.length > len){
				str = str.substring(0,len-2)+"...";
				return str;
			}else{
				return str;
			}
		}else{
			return "";
		}
	}
	function showData(data,type){
		if(type == "detail"){
			$("#student-form-show").contents().find("#save-btns-content").hide();
		}else if(type == "edit"){
			$("#student-form-show").contents().find("#save-btns-content").show();
		}
		for(var key in data){
			if($("#student-form-show").contents().find("#student-" + key)[0]){
				if($("#student-form-show").contents().find("#student-" + key)[0].tagName.toLocaleLowerCase() == "input"){
					if(key=="birdate"){
						$("#student-form-show").contents().find("#student-" + key).val(getSmpFormatDateByLong(data[key], "a"));
					}else if(key=="inschooldate"){
						$("#student-form-show").contents().find("#student-" + key).val(getSmpFormatDateByLong(data[key], "a"));
					}else{
						$("#student-form-show").contents().find("#student-" + key).val(data[key]);
					}
				}else if($("#student-form-show").contents().find("#student-" + key)[0].tagName.toLocaleLowerCase() == "select"){
					if(key=="periodtype"){
						var typeOpt = $("#student-form-show").contents().find("#student-periodtype option");
						for(var i = 0; i< typeOpt.length; i++){
							if(typeOpt[i].getAttribute("value").indexOf(data.periodtype) != -1){
								typeOpt[i].setAttribute("selected",true);
								$("#student-form-show").contents().find("#student-periodtype").change();
							}
						}
						
						var studentOptions = $("#student-form-show").contents().find("#student-grade option");
						for(var i = 0; i< studentOptions.length; i++){
							var classcode = data.classcode;
							if(studentOptions[i].getAttribute("classcodes").indexOf(classcode) != -1){
								studentOptions[i].setAttribute("selected",true);
								$("#student-form-show").contents().find("#student-grade").change();
							}
						}
						
						var classOptions = $("#student-form-show").contents().find("#student-classcode option");
						for(var i = 0; i< classOptions.length; i++){
							if( classOptions[i].getAttribute("value") == data.classcode){
								classOptions[i].selected = true;
							}
						}
					}else{
						var len = $("#student-form-show").contents().find("#student-" + key+" option");
						for(var i = 0; i < len.length ; i++){
							if(len[i].value == data[key]){
								len[i].selected = true;
							}
						}
					}
				}
			}
		}
	}
	
	function updateStudent(){
		var tstudent = getFormData();
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var obj = {};
		for(var key in tstudent){
			if(tstudent[key] != "" && tstudent[key] != undefined){
				obj[key] = tstudent[key];
			}
		}
		
		var data = {
				cmd:"update",
				model:"student",
				userKey:userKey,
				data:obj,
				remark:"",						
		};
		processBegin("正在保存中···");
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("编辑学生信息成功");
				initStudent(currentPage,pageSize);
			}else{
				processError("编辑学生信息失败！");
			}
		},null,null);
	}
	
	function getFormData(){
		new setTStudent(null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null);
		var tstudent = getTStudent();
		var ob = {};
		var obj = {};
		for(var key in tstudent){
			if($("#student-form-show").contents().find("#student-"+key)[0]){
				if($("#student-form-show").contents().find("#student-"+key)[0].tagName.toLocaleLowerCase() == "input"){
					if(key=="birdate"){
						obj[key] = strToDate($("#student-form-show").contents().find("#student-"+key).val());
					}else if(key=="inschooldate"){
						obj[key] = strToDate($("#student-form-show").contents().find("#student-"+key).val());
					}else if(key=="imei"){
						ob[key] = $("#student-form-show").contents().find("#student-"+key).val();
					}else if(key=="cellphone"){
						ob[key] = $("#student-form-show").contents().find("#student-"+key).val();
					}else if(key=="cardcode"){
						ob[key] = $("#student-form-show").contents().find("#student-"+key).val();
					}else{
						obj[key] = encodeURI($("#student-form-show").contents().find("#student-"+key).val());
					}
				}else{
					obj[key] = encodeURI($("#student-form-show").contents().find("#student-"+key+" option:selected").val());
				}
			}
		}
		if(ob.imei.length>0&&ob.cellphone.length>0&&ob.cardcode.length>0){
			ob.studentcode = obj.studentcode;
			obj.tcard = ob;
		}
		obj.schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		new setTStudent(obj.tcard,obj.name,obj.birdate,obj.sex,obj.oldname,obj.country,obj.birplace,obj.nation,obj.political,obj.hukounature,obj.hukounature,obj.familyadress,obj.familyphone,obj.isonechild,obj.phone,obj.grade,obj.classcode,obj.studentcode,obj.inschooldate,obj.distance,obj.waytype,obj.inschooltype,obj.studytype,obj.fname1,obj.fphone1,obj.fcardtype1,obj.fcardid1,obj.fisguardian1,obj.faddress1,obj.frelation1,obj.fnation1,obj.fhukouplacecode1,obj.fcom1,obj.fjob1,obj.fremark1,obj.fname2,obj.fphone2,obj.fcardtype2,obj.fcardid2,obj.fisguardian2,obj.faddress2,obj.frelation2,obj.fnation2,obj.fhukouplacecode2,obj.fcom2,obj.fjob2,obj.fremark2,obj.imei,obj.cellphone,obj.cardcode,obj.schoolcode);
		return getTStudent();	
	}
	
	function check2(){
		var flg = false;
		var checkFails = $("#student-form-show").contents().find(".check-fail");
		for(var i = 0; i < checkFails.length; i++){
			if(checkFails[i].innerHTML != ""){
				flg = false;
				break;
			}else{
				flg = true;
			}
		}
		return flg;
	}
	
	function checkSave(){ 
			if($("#student-form-show").contents().find("#student-name").val() == ""){
				$("#student-form-show").contents().find("#student-name").focus();
				$("#student-form-show").contents().find("#student-name-ex").text("不能为空！");
				return false;
			}else if($("#student-form-show").contents().find("#student-birdate").val() == ""){
				$("#student-form-show").contents().find("#student-birdate").focus();
				$("#student-form-show").contents().find("#student-birdate-ex").text("不能为空！");
				return false;
			}else if($("#student-form-show").contents().find("#student-periodtype option:selected").val() == ""){
				$("#student-form-show").contents().find("#student-periodtype").focus();
				return false;
			}else if($("#student-form-show").contents().find("#student-grade option:selected").val() == ""){
				$("#student-form-show").contents().find("#student-grade").focus();
				return false;
			}else if($("#student-form-show").contents().find("#student-studentcode").val() == ""){
				$("#student-form-show").contents().find("#student-studentcode").focus();
				$("#student-form-show").contents().find("#student-studentcode-ex").text("不能为空！");
				return false;
			}else if($("#student-form-show").contents().find("#student-inschooldate").val() == ""){
				$("#student-form-show").contents().find("#student-inschooldate").focus();
				$("#student-form-show").contents().find("#student-inschooldate-ex").text("不能为空！");
				return false;
			}else if($("#student-form-show").contents().find("#student-fname1").val() == ""){
				$("#student-form-show").contents().find("#student-fname1").focus();
				$("#student-form-show").contents().find("#student-fname1-ex").text("不能为空！");
				return false;			
			}else if($("#student-form-show").contents().find("#student-fphone1").val() == "" ){
				$("#student-form-show").contents().find("#student-fphone1").focus();
				$("#student-form-show").contents().find("#student-fphone1-ex").text("不能为空！");
				return false;
			}else if($("#student-form-show").contents().find("#student-fcardid1").val() == ""){
				$("#student-form-show").contents().find("#student-fcardid1").focus();
				$("#student-form-show").contents().find("#student-fcardid1-ex").text("不能为空！");
				return false;
			}else{
				return true;
			}
	};
	
	function pagebtns(){
		$("#student-name").unbind("keyup");
		$("#student-name").keyup(function(){
			this.value=this.value.replace(/^ +| +$/g,'');
		});
		document.onkeydown = function(e){ 
			$("select").blur();
		    var ev = document.all ? window.event : e;
		    if(ev.keyCode==13) {
		    	now = 0;
				initStudent(1,pageSize);
		    }
		};
		$("#student-search-btn").click(function(){
			now = 0;
			initStudent(1,pageSize);
		});
		$("#downloadModel").click(function(){
			deleteBox("确定要下载吗？",function(){
				window.frames["hrong"].location = Host + "/uploads/excle/models/students-model.xls";		
			});
		});
		$("#leadingIn").click(function(){
			leadingDialog(function(){
				var content =  "<tr style='height:38px;'>" +
									"<td class='table-left' style='width:60px;'><font color='red'>*</font>班级：</td>" +
									"<td class='table-middle' style='width:230px;'>" +
										"<select id='leading-type' style='width:67px;height:22px;'><option value=''>学段</option></select>" +
										"<select id='leading-grade' style='width:67px;height:22px;margin-left:4px;'><option value=''>年级</option></select>" +
										"<select id='leading-class' style='width:67px;height:22px;margin-left:4px;'><option value=''>班级</option></select>" +
									"</td>" +
								"</tr>" +
								"<tr style='height:38px;'>" +
									"<td class='table-left' style='width:60px;'><font color='red'>*</font>文件：</td>" +
									"<td class='table-middle' style='width:230px;'>" +
										"<input type='file' name='file' id='upload' style='height:21px;width:140px;border:none;'>" +
									"</td>" +
								"</tr>";
				$(".leading-dialog-table",parent.parent.parent.document).html(content);
				initDialogClass();
				$("#cancelBtn",parent.parent.parent.document).unbind("click");
				$("#cancelBtn",parent.parent.parent.document).click(function(){
					$(".index-dialog-cover",parent.parent.parent.document).hide();
					$(".leading-dialog",parent.parent.parent.document).hide();
				});
				$("#sureBtn",parent.parent.parent.document).unbind("click");
				$("#sureBtn",parent.parent.parent.document).click(function(){
					$(".index-dialog-cover",parent.parent.parent.document).hide();
					$(".leading-dialog",parent.parent.parent.document).hide();
					leadingInStudents();
				});
			});
		});
		$("#leadingOut").click(function(){
			var content = "";
			var studentType = $("#student-type option:selected").text();
			var studentGrade = $("#student-grade option:selected").text();
			var studentClass = $("#student-classcode option:selected").text();
			var name = $("#student-name").val();
			var islock = $("#student-lock option:selected").val();
			if(studentGrade == "年级" && studentClass == "班级" && studentType == "学段"){
				content += "全部" ;
			}else if(studentType != "学段" && studentGrade == "年级" && studentClass == "班级"){
				content += studentType + "学段";
			}else if(studentType != "学段" && studentGrade != "年级" && studentClass == "班级"){
				content += studentType + studentGrade;
			}else{
				content += studentType + studentGrade + studentClass;
			}
			if(name != ""){
				content += name;
			}
			if(islock == "0" || islock == 0){
				content += "（锁定状态  ）";
			}
			deleteBox("您选择的是   <span class='span-class'>"+content+"</span>  ，</br>确定要导出吗？",function(){
				leadingOutStudents();
			});
		});
	}
	function leadingInStudents(){
		processBegin("正在导入中···");
		var classcode = $("#leading-class option:selected",parent.parent.parent.document).val();
		var schoolid = $("#user-name",parent.parent.document).attr("schoolcode");
		var url = Host + "/service/fileupload.do?clazz=student&classcode="+classcode+"&schoolid="+schoolid;
		var options = {
	            url: url, 
	            type : "POST",
	            success :function(result){
	            	if(result.result == 1){
	            		if(result.data.fail.data == "excel格式错误"){
	            			processError("导入数据格式错误！");
	            		}else if(result.data.fail.data == 1){
	            			processEnd("导入成功!");
	            		}else if(result.data.fail['学生编码存在'] == false){
	            			processError("学生编码存在！");
	            		}
	            	}else{
	            		processError("导入失败");
	            	}
	            },
	    };
	    $("#form",parent.parent.parent.document).ajaxSubmit(options);
	}
	function leadingOutStudents(){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var classcode = $("#student-classcode option:selected").val();
		var islock = $("#student-lock option:selected").val();
		var name = $("#student-name").val();
		var data = {
			cmd: "getAllStudentsByPage",
			model: "student",
			userKey: userKey,
			data:{
				name: name,
				classcode: classcode,
				islock: islock,
				isdownload: "true",
				schoolcode:schoolcode
			},
			remark:"",						
		};
		processBegin("正在导出中···");
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				if(res.data.remark.flag == true){
					var url = Host + res.data.remark.msg;
					window.frames["hrong"].location = url;
					processEnd("导出成功");
				}else{
					processError("导出失败");
				}
			}else{
				processError("暂无数据！");
			}
		},null,null);
	}
	
	function initDialogClass(){
		var typeOpt = "<option value='' >学段</option>";
		var gradeOpt = "<option value='' classcodes='' classes=''>年级</option>";
		var classOpt = "<option value='' classes=''>班级</option>";
		var res = classCaches;
		if(res.result == 1 && res.data != undefined && res.data != ""){
			var data = res.data;
			for(var i = 0; i < data.length; i++){
				var name = "";
				if(data[i].type == "0"){
					name = "幼儿园";
				}else if(data[i].type == "1"){
					name = "小学";
				}else if(data[i].type == "2"){
					name = "中学";
				}else if(data[i].type == "3"){
					name = "高中";
				}
				typeOpt += "<option value='"+data[i].type+"'>"+name+"</option>";
			}
			$("#leading-type",parent.parent.parent.document).html(typeOpt);
			
			$("#leading-type",parent.parent.parent.document).unbind("change");
			$("#leading-type",parent.parent.parent.document).change(function(){
				var gradeData = null;
				var type = $("#leading-type option:selected",parent.parent.parent.document).val();
				var gradeOpt = "<option value='' classcodes='' classes=''>年级</option>";
				if(type != ""){
					for(var i = 0; i < data.length; i++){
						if(data[i].type == type){
							gradeData = data[i].data;
						}
					}
					for(var i = 0; i < gradeData.length; i++){
						gradeOpt += "<option value='"+gradeData[i].grade+"' classcodes='"+gradeData[i].classcode+"' classes='"+gradeData[i].classes+"'>"+gradeData[i].grade+"年级</option>";
					}
				}
				$("#leading-grade",parent.parent.parent.document).html(gradeOpt);
				
				var classOpt = "<option value='' classes=''>班级</option>";
				$("#leading-class",parent.parent.parent.document).html(classOpt);
			});
			
			$("#leading-grade",parent.parent.parent.document).unbind("change");
			$("#leading-grade",parent.parent.parent.document).change(function(){
				var classes = $("#leading-grade option:selected",parent.parent.parent.document).attr("classes").split(",");
				var classcodes = $("#leading-grade option:selected",parent.parent.parent.document).attr("classcodes").split(",");
				var classOpt = "";
				for(var j = 0; j < classes.length; j++){
					if(classcodes[j] == "" && classes[j] == ""){
						classOpt += "<option value='' classes=''>班级</option>";
					}else{
						classOpt += "<option value='"+classcodes[j]+"' classes='"+classes[j]+"'>"+classes[j]+"班</option>";
					}
				}
				$("#leading-class",parent.parent.parent.document).html(classOpt);
			});
		}else{
			$("#leading-type",parent.parent.parent.document).html(typeOpt);
			$("#leading-grade",parent.parent.parent.document).html(gradeOpt);
			$("#leading-class",parent.parent.parent.document).html(classOpt);
		}
		
	}
	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}
})();